//
//  KakaBlackViewController.swift
//  NewsItem
//
//  Created by mac on 2023/7/5.
//  Copyright © 2023 Kaka. All rights reserved.
//

import UIKit

class Pet_BlackViewController: BasicViewController {
    
    var datasource:[InfoUtil.anchorPageList]! = []

    private lazy var listView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 5.Fit
        layout.minimumInteritemSpacing = 5.Fit
        layout.itemSize = .init(width: screenWidth/2 - 40/2, height: 200.Fit)
        let view = UICollectionView(frame: .zero, collectionViewLayout: layout)
        view.register(User_Item.self, forCellWithReuseIdentifier: User_Item.description())
        view.backgroundColor = .clear
        view.contentInset = .init(top: 0, left: 15.Fit, bottom: 0, right: 15.Fit)
        view.delegate = self
        view.dataSource = self
        view.bounces = false
        return view
    }()

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        RT.blockListWith { list in
            self.datasource = list.users
            self.listView.reloadData()
            print("usersusers=======  ")
            print(list.users)
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        title = "Blacklist"
        view.subviews {
            listView
        }
        listView.fillContainer()
        listView.emptyGameView(.noList)
    }
    
}

extension Pet_BlackViewController: UICollectionViewDelegate, UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        self.datasource.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: User_Item.description(), for: indexPath) as! User_Item
        cell.anchormodel = self.datasource[indexPath.row]
        cell.removeB.addAct {
            RT.blockWithUserid(userid: self.datasource[indexPath.row].id ?? "", type: "0") { str in
                self.datasource.remove(at: indexPath.row)
                self.listView.reloadData()
                NotificationCenter.default.post(name: NSNotification.Name("RemoveBLOCKNOTICE"), object: nil)
            }
        }
        return cell
    }
    
    
}



class UserListCell: UITableViewCell {
    
    let backV = UIView.init(frame: .zero)
    let headBtn = UIButton.initV(font: .setTypeFace(20, type: .bold), text: "", img: "placeavar",contentMent: .left)
    private var nameL = UILabel.initV(.setTypeFace(16, type: .bold), text: "", textColor: mainColor)
    private var descL = UILabel.initV(.setTypeFace(12, type: .medium), text: "", textColor: mainColor)
    let more = UIButton.initV(font: .setTypeFace(16, type: .extraBold), text: "Remove", img: "",contentMent: .center)
    var model:InfoUtil.anchorPageList?{
        didSet{
            self.nameL.text = model?.name
            if let url = URL(string: model?.headImage ?? "") {
                self.headBtn.kf.setImage(with: url, for: .normal)
            }
            self.descL.text = model?.userDesc
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        backgroundColor = .clear
        selectionStyle = .none
        contentView.subviews {
            backV.subviews {
                headBtn
                nameL
                descL
                more
            }
        }
        
        backV.isUserInteractionEnabled = true
        backV.left(15.Fit).top(15.Fit).right(15.Fit).height(80.Fit)
        backV.backgroundColor = .color(hexString: "#EDF2DC",alpha: 1)
        backV.setAllCorners(corner: 13.Fit)

        headBtn.left(15.Fit).top(15.Fit).width(50.Fit).height(50.Fit)
        headBtn.setAllCorners(corner: 25.Fit)
        
        more.trailing(15).height(40.Fit).width(90.Fit)
        more.centerVertically()
        more.backgroundColor = mainColor
        more.setAllCorners(corner: 10.Fit)
        more.setTitleColor(.white, for: .normal)
        
        nameL.top(20.Fit).height(20.Fit)
        nameL.Leading == headBtn.Trailing + 10.Fit
        nameL.Trailing == more.Leading - 10.Fit

        descL.height(20.Fit)
        descL.Leading == headBtn.Trailing + 10.Fit
        descL.Trailing == more.Leading - 10.Fit
        descL.Top == nameL.Bottom + 5.Fit
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
